define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'cash/index' + location.search,
                    add_url: 'cash/add',
                    edit_url: 'cash/edit',
                    del_url: 'cash/del',
                    multi_url: 'cash/multi',
                    import_url: 'cash/import',
                    table: 'cash',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'main_type', title: __('Main_type'), searchList: {"1":__('Main_type 1'),"2":__('Main_type 2')}, formatter: Table.api.formatter.normal},
                        {field: 'main_id', title: __('Main_id')},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {field: 'day_limit', title: __('Day_limit')},
                        {field: 'money', title: __('Money'), operate: 'LIKE'},
                        {field: 'reward', title: __('Reward'), operate: 'LIKE'},
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"2":__('Status 2')}, formatter: Table.api.formatter.status},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on('change', "#c-policy", function () {
                let val = $(this).val();
                Fast.api.ajax({
                    url: 'cash/ajax',
                    data: {id: val},
                    loading: false
                }, function (data) {
                    let cash = data['cash'];
                    $("#c-day_limit").val(cash['day_limit']);
                    $("#c-money").val(cash['money']);
                    $("#c-reward").removeAttr("data-rule");
                    $("#c-reward").attr("data-rule","required&range(0~"+cash['reward']+")");

                    $("#c-product_id").val(data.product_id);
                    $("#c-product_id").selectPageRefresh();
                    Fast.api.ajax({
                        url: "profit/list",
                        data: {product_id: data.product_id,profit_id: data.profit_id}
                    }, function (data) {
                        var rate = data.rate;
                        var card_type = data.card_type;
                        var tran = data.transaction_type;
                        var channels = data.channels;
                        $("#c-rate_ids,#c-card_type,#c-transaction_type_ids,#c-channel_ids").selectPageClear();
                        $("#c-rate_ids").selectPageData(rate);
                        $("#c-card_type").selectPageData(card_type);
                        $("#c-transaction_type_ids").selectPageData(tran);
                        $("#c-channel_ids").selectPageData(channels);
                        $("#c-rate_ids,#c-card_type,#c-transaction_type_ids,#c-channel_ids").selectPageRefresh();
                        return false;
                    });
                    return false;
                }, function () {
                    return false;
                });
            });
            $(document).on('change', "#c-product_id", function () {
                var product_id = $(this).val();
                Fast.api.ajax({
                    url: "profit/list",
                    data: {product_id: product_id},
                }, function (data, ret) {
                    var rate = data.rate;
                    var card_type = data.card_type;
                    var tran = data.transaction_type;
                    var channels = data.channels;
                    $("#c-rate_ids,#c-card_type,#c-transaction_type_ids,#c-channel_ids").selectPageClear();
                    $("#c-rate_ids").selectPageData(rate);
                    $("#c-card_type").selectPageData(card_type);
                    $("#c-transaction_type_ids").selectPageData(tran);
                    $("#c-channel_ids").selectPageData(channels);
                    $("#c-rate_ids,#c-card_type,#c-transaction_type_ids,#c-channel_ids").selectPageRefresh();
                    return false;
                });
                return false;
            });
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
